package com.zyz.K_集合.Collections;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

/**
 * @author: vick
 * @Date: 2025/3/31
 * @Description:
 * @version: 1.0
 */
public class Demo1 {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        // 将所有指定的元素添加到 collection 中
        Collections.addAll(collection, "hello", "vick");
        Collections.addAll(collection, 1, 2, 3, 4);
        System.out.println(collection);

        System.out.println("===自然排序===");
        ArrayList arrayList = new ArrayList();
        arrayList.add(new User("Alice", 30));
        arrayList.add(new User("Vick", 28));
        arrayList.add(new User("Tom", 27));
        arrayList.add(new User("aaaa", 28));


        Collections.sort(arrayList);
        System.out.println(arrayList);

        System.out.println("===定制排序===");
        Collections.sort(arrayList, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return -(o1.getAge() - o2.getAge());
            }
        });
        System.out.println(arrayList);
        System.out.println("===元素交换===");
        //将指定集合 i和j处元素交换
        ArrayList arrayList1 = new ArrayList();
        // 将所有指定的元素添加到 collection 中
        Collections.addAll(arrayList1, "hello", "vick","java");
        Collections.swap(arrayList1, 0, 2);
        System.out.println(arrayList1);

        System.out.println("===复制===");
        ArrayList<Integer> arrayList2 = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            arrayList2.add(i);
        }
        ArrayList<Integer> arrayList3 = new ArrayList<>();
        for (int i = 11; i <= 13; i++) {
            arrayList3.add(i);
        }

        System.out.println(arrayList2);
        System.out.println(arrayList3);
        Collections.copy(arrayList2, arrayList3);
        System.out.println(arrayList2);


        System.out.println("===替换===");
        ArrayList<String> arrayList4 = new ArrayList<>();
        Collections.addAll(arrayList4, "hello", "vick","java","Python","C++","hello");
        Collections.replaceAll(arrayList4, "hello","vvvvvv");
        System.out.println(arrayList4);
    }
}
